package com.example.lowbird.lowbird.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.bean.HotBean;
import com.example.lowbird.lowbird.java.SHopcar;
import com.example.lowbird.lowbird.utils.Animatorutils;
import com.example.lowbird.lowbird.utils.Dbutils;
import com.example.lowbird.lowbird.utils.ZhendongUtils;
import com.example.lowbird.lowbird.view.Shoptext;

import java.util.List;

/**
 * Created by Administrator on 2016/11/3.
 */
public class Goodadapter extends RecyclerView.Adapter<Goodadapter.GoodViewHolder> {

    private Context mContext;

    private LayoutInflater inflater;

    private HotBean hotBean;
    boolean flag = true;
    List<HotBean.ListBean> data;

    public Goodadapter(Context mContext, HotBean hotBean) {
        this.mContext = mContext;

        inflater = LayoutInflater.from(mContext);
        this.hotBean = hotBean;

        data = hotBean == null ? null : hotBean.getList();
    }

    @Override
    public GoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new GoodViewHolder(inflater.inflate(R.layout.adapter_hot, null));
    }

    @Override
    public void onBindViewHolder(GoodViewHolder holder, int position) {


        final HotBean.ListBean listBean = data.get(position);

        Glide.with(mContext).load(listBean.getImgUrl()).diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade().into(holder.iv);
        holder.tv1.setText(listBean.getName());
        holder.tv2.setText("¥ " + listBean.getPrice());



    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void addhotbean(HotBean hotBean1) {

        if (data==null){
            data=hotBean1.getList();
        }else {

            data.addAll(hotBean1.getList());
        }

        notifyDataSetChanged();
    }

    public void setlayout() {

        notifyDataSetChanged();
    }


    class GoodViewHolder extends RecyclerView.ViewHolder {


        private ImageView iv;
        private TextView tv1;
        private TextView tv2;
        private Button bt;


        public GoodViewHolder(View view) {
            super(view);

            view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));



            iv = ((ImageView) view.findViewById(R.id.iv_hotadapter));

            tv1 = ((TextView) view.findViewById(R.id.tv_hotadapter));
            tv2 = ((TextView) view.findViewById(R.id.tv_hotadapter_price));
            bt = ((Button) view.findViewById(R.id.bt_hotbuy));



            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = getLayoutPosition();
                    HotBean.ListBean listBean = data.get(layoutPosition);
                    Log.e("HOT", "onClick: " + listBean.getPrice());

                    ZhendongUtils.vSimple(mContext, 20);

                    Dbutils.createShop(listBean.getId(), listBean.getImgUrl(), listBean.getName(), 1, listBean.getPrice(), 15, "");


                }
            });
            View.OnClickListener listener=new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    int layoutPosition = getLayoutPosition();
                    HotBean.ListBean listBean = data.get(layoutPosition);


                    SHopcar shopcar=new SHopcar(listBean.getId(),listBean.getImgUrl(),listBean.getName()
                    ,1,listBean.getPrice(),15,"",1);

                    Animatorutils.initanimation(mContext,iv,shopcar);



                }
            };

            iv.setOnClickListener(listener);
            tv1.setOnClickListener(listener);
        }
    }
}
